package com.jumi.microservice.config;

import com.jumi.microservice.common.redis.RedisCache;
import com.jumi.microservice.jwt.ignore.IgnoreProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration
@DependsOn("ignorePostProcess")
public class CpsRedisInit {
    @Autowired
    RedisCache redisService;

    @PostConstruct
    public void init() {
        initIgnoreUrl();
    }

    public void initIgnoreUrl() {
        System.err.println("启动添加忽略接口至Redis----------");
        List<String> ignoreUrls = IgnoreProperties.ignoreUrls;//本项目
        List<Object> cacheList = redisService.getCacheList(IgnoreProperties.REDIS_IGNORE_KEY);//redis中所有的白名单
        List<Object> newIgnoreUrls = new ArrayList<>();
        for (String ignoreUrl : ignoreUrls) {
            if (!cacheList.contains(ignoreUrl)) {
                newIgnoreUrls.add(ignoreUrl);
            }
        }
        if (newIgnoreUrls.size() > 0) {
            redisService.setCacheList(IgnoreProperties.REDIS_IGNORE_KEY, newIgnoreUrls);
        }
    }
}
